package com.zzb.weikeautosign.console;

import org.apache.log4j.Logger;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 终端控制
 * Created by zzb on 16/2/18.
 */
public class ConsoleThread extends Thread {
    private static final Logger log = Logger.getLogger(ConsoleThread.class);

    private static ConsoleThread thread = null;

    public static ConsoleThread getInstance() {
        if (thread == null) thread = new ConsoleThread();
        return thread;
    }

    static Pattern helpPattern = Pattern.compile("^(\\?|help)$");
    static Pattern exitPattern = Pattern.compile("^(q|quit|exit|bye)$");
    static Pattern addP = Pattern.compile("^add.*$");
    static Pattern addUserP = Pattern.compile("-u (.*) (-p.*)$");
    static Pattern addPassP = Pattern.compile("-p (.*)$");
    static Pattern addPassPWithType = Pattern.compile("-p (.*) (-t.*)$");
    static Pattern addTypeP = Pattern.compile("-t (.*)$");
    static Pattern delP = Pattern.compile("^del.*$");
    static Pattern delUserP = Pattern.compile("^del (.*)$");
    @Override
    public void run() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            if (scanner.hasNext()) {
                String s = scanner.nextLine();
                ConsoleExcutor.execute(s);
            }
        }
    }

    public static void main(String[] args) {
        new ConsoleThread().start();
        String s = "add -u 123 -p 321";
        Matcher m = addUserP.matcher(s);
        while (m.find()) {
            System.out.println(m.group());
            System.out.println(m.group(1));
            System.out.println("-------");
        }

        Matcher m2 = addPassPWithType.matcher(s);
        while (m2.find()) {
            System.out.println(m2.group());
            System.out.println(m2.group(1));
            System.out.println("-------");
        }

    }
}
